#!/bin/bash

UX_INSTALL_DIR="/segfs/tango/ci/Tango"
WIN_INSTALL_DIR="//unixhome/segfs/tango/ci/Tango"

#
# Is clean required ?
#

if [ $# = 1 ]
then
	if [ $1 = "clean" ]
	then
		CLEAN="clean"
	fi
fi

#
# Set some variables according to OS
#

for i in $NODE_LABELS
do
	echo $i

	if [ $i = "ubuntu12.04" ]
	then
		echo "Ubuntu 12.04 OS"
		OS_TYPE="linux"
		OS_SPEC="linux=1 ubuntu=1"
		MAKE_PATH="make"
		INSTALL_DIR=$UX_INSTALL_DIR
	fi

	if [ $i = "Windows_VC9" ]
	then
		echo "Windows VC9"
		OS_TYPE="Windows"
		OS_SPEC="Windows"
		MAKE_PATH="devenv"
		INSTALL_DIR=$WIN_INSTALL_DIR
	fi

	if [ $i = "Windows_VC10" ]
	then
		echo "Windows VC10 (64 bits)"
		OS_TYPE="Windows"
		OS_SPEC="Windows_64_VC10"
		MAKE_PATH="MSBuild.exe"
		INSTALL_DIR=$WIN_INSTALL_DIR
	fi
	
	if [ $i = "centos5" ]
	then
		echo "Centos 5 OS"
		OS_TYPE="linux"
		OS_SPEC="linux=1"
		MAKE_PATH="make"
		INSTALL_DIR=$UX_INSTALL_DIR
	fi

	if [ $i = "redhate4" ]
	then
		echo "Red Hat 4 (32 bits) OS"
		OS_TYPE="linux"
		OS_SPEC="linux=1"
		MAKE_PATH="make"
		INSTALL_DIR=$UX_INSTALL_DIR
	fi

	if [ $i = "redhate5" ]
	then
		echo "Red Hat 5 (64 bits) OS"
		OS_TYPE="linux"
		OS_SPEC="linux=1"
		MAKE_PATH="make"
		INSTALL_DIR=$UX_INSTALL_DIR
	fi

	if [ $i = "debian6" ]
	then
		echo "Debian 6 (64 bits) OS"
		OS_TYPE="linux"
		OS_SPEC="linux=1"
		MAKE_PATH="make"
		INSTALL_DIR=$UX_INSTALL_DIR
	fi

	if [ $i = "debian7_tango" ]
	then
		echo "Debian 7 (64 bits) OS"
		OS_TYPE="linux"
		OS_SPEC="linux=1"
		MAKE_PATH="make"
		INSTALL_DIR=$UX_INSTALL_DIR
	fi

	if [ $i = "debian8_tango" ]
	then
		echo "Debian 8 (64 bits) OS"
		OS_TYPE="linux"
		OS_SPEC="linux=1"
		MAKE_PATH="make"
		INSTALL_DIR=$UX_INSTALL_DIR
	fi
done

#
# Set library type
#

if [ $OS_TYPE = "Windows" ]
then
	if [ $LIBTYPE = "shared" ]
	then
		PROJ_NAME="win32_ds_dll"
		PROJ2_NAME="fwd_ds_dll"
		OUTFILE="devenv_ds_dll"
	else
		PROJ_NAME="win32_ds_static"
		PROJ2_NAME="fwd_ds_static"
		OUTFILE="devenv_ds_static"
	fi
else
	if [ $LIBTYPE = "shared" ]
	then
		LIB_TYPE="shared=1"
	fi
fi

#
# Build make command
#

if [ $OS_TYPE = "Windows" ]
then
	if [ -z $CLEAN ]
	then
		BUILD="/build"
	else
		if [ $OS_SPEC = "Windows_64_VC10" ]
		then
			PROJ_NAME="Clean;"$PROJ_NAME
		else
			BUILD="/rebuild"
		fi
	fi
	export PREFIX=$INSTALL_DIR
	if [ $OS_SPEC = "Windows_64_VC10" ]
	then
		MAKE_CMD="$MAKE_PATH WIN32_DS.sln /m /p:Platform=x64 /t:$PROJ_NAME;$PROJ2_NAME /p:Configuration=Debug /v:quiet /flp:LogFile=$OUTFILE;Summary;ShowCommandLine;Verbosity=minimal"
		export PATH=$PATH:/cygdrive/c/Windows/Microsoft.Net/Framework64/v4.0.30319
		cd cpp_test_ds/win64_ds_vc10
	else
		MAKE_CMD="$MAKE_PATH WIN32_DS.sln /project $PROJ_NAME $BUILD Debug  /projectconfig Debug /out $OUTFILE "
		MAKE2_CMD="$MAKE_PATH WIN32_DS.sln /project $PROJ2_NAME $BUILD Debug  /projectconfig Debug /out $OUTFILE "
		export PATH=$PATH:/cygdrive/c/Program\ Files/Microsoft\ Visual\ Studio\ 9.0/Common7/IDE
		cd cpp_test_ds/win32_ds_vs9
	fi
	echo $PATH
	/bin/rm -f $OUTFILE
else
	MAKE_CMD="$MAKE_PATH prefix=$INSTALL_DIR $OS_SPEC $LIB_TYPE $CLEAN all"
	cd cpp_test_ds
fi

#
# Build DS
#
echo $MAKE_CMD
$MAKE_CMD
RET=$?
if [ $OS_SPEC = "Windows" ]
then
	echo $MAKE2_CMD
	$MAKE2_CMD
fi

#
# For windows, in the archive build, also build DS running with its own window
#

if [ $OS_TYPE = "Windows" ]
then
	if [ $LIBTYPE = "archive" ]
	then
		/bin/cat $OUTFILE

		PROJ_NAME="win32_ds_win"
		OUTFILE="devenv_ds_win"
		if [ $OS_SPEC = "Windows_64_VC10" ]
		then
			MAKE_CMD="$MAKE_PATH WIN32_DS.sln /p:Platform=x64 /t:$PROJ_NAME /p:Configuration=Debug /v:quiet /flp:LogFile=$OUTFILE;Summary;ShowCommandLine;Verbosity=minimal"
		else
			MAKE_CMD="$MAKE_PATH WIN32_DS.sln /project $PROJ_NAME /build Debug /projectconfig Debug /out $OUTFILE"
		fi
		/bin/rm -f $OUTFILE

		echo $MAKE_CMD
		$MAKE_CMD
		RET=$?
	fi
fi

if [ $OS_TYPE = "Windows" ]
then
	/bin/cat $OUTFILE
fi

if [ $RET != 0 ]
then
	exit -1
fi
